package com.zrt.bookstore3.Controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zrt.bookstore3.Controller.utils.R;
import com.zrt.bookstore3.Domain.Book;
import com.zrt.bookstore3.Domain.Order;
import com.zrt.bookstore3.Domain.User;
import com.zrt.bookstore3.Service.BookService;
import com.zrt.bookstore3.Service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@RestController
@RequestMapping("/orders")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PutMapping("/updateOrder")
    public R updateOrder(@RequestBody Order order) throws IOException {
        //if (user.getUsername().equals("123") ) throw new IOException();
        boolean flag = orderService.updateOrder(order);
        return new R(flag, flag ? "修改成功^_^" : "修改失败-_-!");
    }

    @GetMapping("/select/{currentPage}/{pageSize}")
    public R getPage(@PathVariable int currentPage, @PathVariable int pageSize, Order order){
        System.out.println("参数==>"+order);
        System.out.println("哈哈哈哈哈哈哈哈哈哈哈哈");
        IPage<Order> page = orderService.getPage(currentPage, pageSize,order);
        //如果当前页码值大于了总页码值，那么重新执行查询操作，使用最大页码值作为当前页码值
        if( currentPage > page.getPages()){
            page = orderService.getPage((int)page.getPages(), pageSize,order);
        }
        return new R(true, page);
    }

    @PostMapping("/listOrderByCondition")
    public R listOrderByCondition(@RequestBody Order order){
        return new R(true,orderService.selectByCondition(order));
    }

    @PutMapping("/updateStatusAndNumber")
    public R update(@RequestBody Order order) throws IOException {
        boolean flag = orderService.updateStatusAndNumber(order);
        return new R(flag, flag ? "修改成功^_^" : "修改失败-_-!");
    }

    @GetMapping("/selectById/{id}")
    public R getByIdForSelect(@PathVariable Integer id){
        return new R(true, orderService.selectById(id));
    }


    @DeleteMapping("{id}")
    public R delete(@PathVariable Integer id){
        return new R(orderService.deleteById(id));
    }


    @GetMapping("/selectAllData")
    public R getAllData(HttpServletRequest request){
        return new R(true,orderService.selectAllData());
    }

    @GetMapping("/selectAllPay")
    public R getAllPay(HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("user");
        return new R(true,orderService.selectAllPay(user.getId()));
    }

    @GetMapping("/updateStatus/{id}")
    public R getById(@PathVariable Integer id){
        System.out.println("----------------------------------");
        System.out.println(id);
        System.out.println("----------------------------------");
        return new R(true, orderService.updateStatus(1,id));
    }

    @GetMapping("/selectAllPurchase")
    public R getAll(HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("user");
        return new R(true,orderService.selectAllByUid(user.getId()));
    }

    @PostMapping
    public R save(@RequestBody Order order, HttpServletRequest request) throws IOException {
        System.out.println("********************");
        System.out.println(order.getBookId());
        User user = (User) request.getSession().getAttribute("user");
        int bookId = (int)request.getSession().getAttribute("bookId");
        System.out.println(user);
        int uid = user.getId();
        order.setUid(uid);
        order.setBookId(bookId);
        order.setStatus(0);
        order.setPayTime(null);
        System.out.println("********************");
        boolean flag = orderService.save(order);
        return new R(flag, flag ? "添加到购物车成功^_^" : "添加到购物车失败-_-!");
    }
}
